package cn.lyf.controller;

import cn.lyf.entity.Blog;
import cn.lyf.entity.BlogType;
import cn.lyf.entity.Body;
import cn.lyf.entity.User;
import cn.lyf.respjson.RespJson;
import cn.lyf.service.BlogService;
import cn.lyf.util.DateFormat;
import cn.lyf.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
<<<<<<< HEAD
@RequestMapping("/api/user/blog")
=======
@RequestMapping("/api/blog")
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
public class BlogController {
    @Autowired
    BlogService blogService;

    //跳转到写博客的页面
    @RequestMapping("/go2Blog")
    public String go2Blog(){
        return "blog/blog";
    }

    //写博客
    @PostMapping("/addBlog")
    @ResponseBody
    public Map<String,Object> addBlog(@RequestBody Map<String,Object> map){
<<<<<<< HEAD
        User user = SessionUtils.getUser();
        if(user==null)return RespJson.pack(401,"未登录",null);
=======
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
        Body body = new Body();
        Blog blog = new Blog();
        body.setContent((String) map.get("content"));
        blog.setMsg((String)map.get("msg"));
        blog.setTitle((String)map.get("title"));
        blog.setBlogtypeid((String)map.get("blogtypeid"));
        blog.setTcreate(DateFormat.dateFormat());//设置发表时间
<<<<<<< HEAD
        blog.setAuthor(user.getNickname());//获取登录用户的名字(也就是获取发表博客的作者名)
        blog.setUid(user.getId());
        blog.setReadnum(0);//阅读数初始化为0
        blog.setCommentnum(0);//评论数初始化为0
=======
        blog.setAuthor(SessionUtils.getNickrname());//获取登录用户的名字(也就是获取发表博客的作者名)
        blog.setReadnum(0);//阅读数初始化为0
        blog.setAnswernum(0);//评论数初始化为0
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
        blog.setLikenum(0);//评论数初始化为0
        StringBuilder sb = new StringBuilder();
        sb.append("摘要:本文由");
        sb.append(blog.getAuthor());
        sb.append("分享,原题\"");
        sb.append(blog.getTitle()+"\""+"。");
        //如果从前端传来的纯文本长度大于等于50个字
        String msg = blog.getMsg();
        if(msg.length()>=50){
            sb.append(msg.substring(0,50));
        }else{//小于50个字符就直接添加
            sb.append(msg);
        }
        sb.append("...");
        blog.setMsg(sb.toString());
        blogService.add(body,blog);
        return RespJson.pack(200,"提交成功",null);
    }

<<<<<<< HEAD

    //点赞
    @GetMapping("/addLike/{blogid}")
=======
    //获取用户
    @RequestMapping("/getUser")
    @ResponseBody
    public Map<String,Object> getUser(){
        User user = SessionUtils.getUser();
        return RespJson.pack(200,"",user);
    }

    //点赞
    @RequestMapping("/addLike/{blogid}")
>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
    @ResponseBody
    public Map<String,Object> addLike(@PathVariable("blogid")String blogid){
        //获取登录用户
        User user = SessionUtils.getUser();
        if(user==null)return RespJson.pack(401,"未登录不能点赞",null);
        int n = blogService.addLike(blogid, user.getId());
        return n==1?RespJson.pack(200,"",null):RespJson.pack(400,"",null);
    }

<<<<<<< HEAD
    //查找我的所有博客
    @GetMapping("/findMyBlogs")
    @ResponseBody
    public Map<String,Object> findMyBlogs(){
        //获取登录用户
        User user = SessionUtils.getUser();
        if(user==null)return RespJson.pack(401,"未登录",null);
        return RespJson.pack(200,"",blogService.findBlogsByUid(user.getId()));
    }

    //根据博客id删除某篇博客
    @GetMapping("/delById/{blogid}")
    @ResponseBody
    public Map<String,Object> delById(@PathVariable("blogid")String blogid){
         blogService.delById(blogid);
        return RespJson.pack(200,"",null);
    }

    //根据博客id查找该博客的评论
    @GetMapping("/readCommentsById/{blogid}")
    @ResponseBody
    public Map<String,Object> readCommentsById(@PathVariable("blogid")String blogid){
        System.out.println("blogid=="+blogid);
        System.out.println("comments=="+blogService.findCommentsById(blogid));
        return RespJson.pack(200,"",blogService.findCommentsById(blogid));
    }
=======



>>>>>>> a254f66694e58548627c0548f6e222a03570bbfd
}
